#*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*#
#          Copyright (C) 2011 Regents of the University of Colorado.           #
#                             All Rights Reserved.                             #
#                                                                              #
#    This program is free software: you can redistribute it and/or modify      #
#    it under the terms of the GNU General Public License version 2 as         #
#    published by the Free Software Foundation.                                #
#                                                                              #
#    This program is distributed in the hope that it will be useful,           #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of            #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
#    GNU General Public License for more details.                              #
#                                                                              #
#    You should have received a copy of the GNU General Public License         #
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     #
#                                                                              #
#           Jack Elston                       Cory Dixon                       #
#           elstonj@colorado.edu              dixonc@colorado.edu              #
#                                                                              #
#*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*#

#--------------------------------------------------------------------- 
#
# Makefile 
#   for Avionics Project
#
# Make sure your environment variable AUGNET is set
# to where you are building the software at.  It should point
# to the software directory withing the Avionics project
#
# This file needs the Makefile.include file which contains
# all of the file dependecies for the directory of which
# it is in.
#
# Author: Cory Dixon
#
#--------------------------------------------------------------------- 

.SILENT:
NO_L2_RADAR=1
NO_VPS=1

#--------------------------------------------------------------------- 
# System config variables 
#--------------------------------------------------------------------- 
#ifneq (,$(findstring CHANGEROOT,$(PS1)))
ifdef ARCH_ARM
 EMBEDDED=1
endif

ifdef ARCH_GEODE
 EMBEDDED=1
endif

ifdef EMBEDDED
 NO_KML=1
 NO_XML=1
 NO_SVN=1
 NO_GUI=1
 NO_L2_RADAR=1
 NO_VPS=1
 TARGET_FLAGS = -DIS_TARGET
endif

NO_KML=1
NO_XML=1
NO_SVN=1
NO_GUI=1
NO_L2_RADAR=1
NO_VPS=1

SYS_FLAGS = -DUSE_GDK_THREAD

#--------------------------------------------------------------------- 
# default - executable files
#--------------------------------------------------------------------- 
default: exe

#--------------------------------------------------------------------- 
# Environment Variables
#--------------------------------------------------------------------- 
ifdef DEBUG
  DBPRINT = -DDEBUG_PRINT
else
  DBPRINT = -DNDEBUG
endif

#---------------------------------------------------------------------
# Makefile.include
# include directory specific makefile
#---------------------------------------------------------------------
-include Makefile.include Makefile.depend

VPATH  = $(DIRS) $(OBJDIR)
INCDIR = $(DIRS)

#---------------------------------------------------------------------
# macros
#---------------------------------------------------------------------
INCLUDES  = $(addprefix -I,$(INCDIR)) -I. -I/usr/armv7a-softfloat-linux-gnueabi/embedded_fs/usr/include
FLAGS     = -ggdb -O0 -D_REENTRANT -Wall $(SYS_FLAGS)
LDLIBS    = -lpthread -lrt -ltrace $(EXTRA_LIBS)
CC        = gcc
CXX       = g++

ifdef ARCH_ARM
CC        = armv7a-softfloat-linux-gnueabi-gcc
CXX       = armv7a-softfloat-linux-gnueabi-g++
FLAGS     = -ggdb -O0 -D_REENTRANT -Wall $(SYS_FLAGS) -Os -pipe -msoft-float
LDLIBS    = -lpthread -lrt -ltrace $(EXTRA_LIBS) -L/usr/armv7a-softfloat-linux-gnueabi/embedded_fs/usr/lib -Wl,-rpath,"/usr/armv7a-softfloat-linux-gnueabi/embedded_fs/usr/lib"
endif

ifdef ARCH_GEODE
CC        = i486-pc-linux-gnu-gcc
CXX       = i486-pc-linux-gnu-g++
LDLIBS    = -lpthread -lrt -ltrace $(EXTRA_LIBS) -L/usr/i486-pc-linux-gnu/embedded_fs/usr/lib -Wl,-rpath,"/usr/i486-pc-linux-gnu/embedded_fs/usr/lib"
endif

CFLAGS    = $(FLAGS) $(DBPRINT) $(INCLUDES) $(EXTRA_FLAGS) $(TARGET_FLAGS)
CXXFLAGS  = -Wno-deprecated -ffriend-injection $(FLAGS) $(DBPRINT) $(INCLUDES) $(EXTRA_FLAGS) $(TARGET_FLAGS)

#=====================================================#
# Display Codes (this is so we can track passes)
#=====================================================#
SETCOLOR_RED   = @echo -en "\\033[0;31m"
SETCOLOR_GREEN = @echo -en "\\033[0;32m"
SETCOLOR_BLUE  = @echo -en "\\033[0;34m"
SETCOLOR       = @echo -en "\\033[0;m"

#--------------------------------------------------------------------- 
# Executable files
#--------------------------------------------------------------------- 
exe: $(PROGS)

#---------------------------------------------------------------------
# Inference rules for executables
#---------------------------------------------------------------------
$(PROGS): 
	$(SETCOLOR_GREEN)
	@echo "--> $@" 
	$(SETCOLOR)
	$(CXX) $^ -o $@ $(LDLIBS)

#---------------------------------------------------------------------
# Utility commands
#---------------------------------------------------------------------
strip: $(PROGS)
	strip --strip-all $^

splint: $(SRC_FILES)
	splint ${CXXFLAGS} $^

xml: $(AUTO_SRC) 

Makefile.depend: Makefile Makefile.include $(AUTO_SRC) 
	$(SETCOLOR_RED)
	@echo -n " == Generating dependency list ... "
	$(SETCOLOR)
	rm -f Makefile.depend
	touch Makefile.depend
	if [ ! -d dep/ ]; then mkdir dep/; fi;
	for file in $(SRC_FILES); do \
		rm -f dep/$${file}.dep; \
		echo "-include dep/$${file}.dep" >> Makefile.depend; \
		echo "dep/$${file}.dep: $${file}" >> Makefile.depend; \
		echo "	\$$(SETCOLOR_RED)" >> Makefile.depend; \
		echo "	echo \"    \$$@\"" >> Makefile.depend; \
		echo "	\$$(SETCOLOR)" >> Makefile.depend; \
		echo "	echo -n \"\$$(OBJDIR)/\" > dep/$${file}.dep" >> Makefile.depend; \
		echo "	\$${CXX} \$${CXXFLAGS} -MM \$$^ >> dep/$${file}.dep" >> Makefile.depend; \
		echo "" >> Makefile.depend; \
	done
	$(SETCOLOR_RED)
	echo "[DONE]"
	$(SETCOLOR)

#---------------------------------------------------------------------
# Inference rules for objects from files with a .cxx extension
#---------------------------------------------------------------------
#% : %.cxx %.h
#	$(SETCOLOR_GREEN)
#	@echo "  --> $@"
#	$(SETCOLOR)
#	$(CXX) $(CXXFLAGS) $(LDLIBS) $< -o $@
#
#% : %.cxx 
#	$(SETCOLOR_GREEN)
#	@echo "  --> $@"
#	$(SETCOLOR)
#	$(CXX) $(CXXFLAGS) $(LDLIBS) $< -o $@

$(OBJDIR)/%.o : %.cxx %.h
	$(SETCOLOR_BLUE)
	@echo "     $@"
	$(SETCOLOR)
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(OBJDIR)/%.o : %.cpp %.h
	$(SETCOLOR_BLUE)
	@echo "     $@"
	$(SETCOLOR)
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(OBJDIR)/%.o : %.cxx
	$(SETCOLOR_BLUE)
	@echo "     $@"
	$(SETCOLOR)
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(OBJDIR)/%.o : %.cpp
	$(SETCOLOR_BLUE)
	@echo "     $@"
	$(SETCOLOR)
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(OBJDIR)/%.o : %.c
	$(SETCOLOR_BLUE)
	@echo "     $@"
	$(SETCOLOR)
	$(CXX) $(CFLAGS) -c $< -o $@

#--------------------------------------------------------------------- 
# Install 
#--------------------------------------------------------------------- 
NETUAS_BASEDIR="/opt/netuas"
install: exe
	mkdir -p $(NETUAS_BASEDIR)/{bin,gui,logs,maps,radar,pas,boundaries,flight_plans,screenshots}
	cp -av mnrDaemon     $(NETUAS_BASEDIR)/bin/
	cp -av gatewayDaemon $(NETUAS_BASEDIR)/bin/
	cp -av piccoloDaemon $(NETUAS_BASEDIR)/bin/
	cp -av $(CDIR)/vcgui3/*.xml $(NETUAS_BASEDIR)/gui/
	cp -av $(CDIR)/vcgui3/nexrad-stations.txt $(NETUAS_BASEDIR)/radar/
	cp -av $(CDIR)/vcgui3/pas/*.png $(NETUAS_BASEDIR)/pas/
	cp -av $(CDIR)/ant_point/WMM.COF /usr/local/share
	cp -av $(CDIR)/ant_point/EGM9615.BIN /usr/local/share

#--------------------------------------------------------------------- 
# Clean 
#--------------------------------------------------------------------- 
clean: 
	$(SETCOLOR_BLUE)
	@echo  " == CLEAN"
	$(SETCOLOR)
	rm -f $(OBJDIR)/*.o core* *.out *.dat *.log *Fifo; 

allclean: clean
	$(SETCOLOR_RED)
	@echo " == DEEP CLEAN"
	@echo "    -- XML autogen"
	$(SETCOLOR)
	rm -f $(EXTRA_RM);
	rm -f $(PROGS) Makefile.depend dep/*.dep
